package com.example.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.domain.entity.Log;
import com.example.domain.entity.Params;
import com.example.mapper.LogMapper;
import com.example.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class LogServiceImpl implements LogService {

//    @Resource
//    private LogDao logDao;

    @Autowired
    private LogMapper logMapper;
    
    /**
     * 分页查询
     * @param params
     * @return
     */
    public Page<Log> findBySearch(Params params) {
        // 开启 MyBatis-Plus 分页查询
        Page<Log> page = new Page<>(params.getPageNum(), params.getPageSize());

        // 构建查询条件
        QueryWrapper<Log> queryWrapper = new QueryWrapper<>();
        if (params != null) {
            if (params.getName() != null && !params.getName().isEmpty()) {
                queryWrapper.like("name", params.getName());
            }
            if (params.getUsername() != null && !params.getUsername().isEmpty()) {
                queryWrapper.like("username", params.getUsername());
            }
        }
        // 按 id 倒序排序
        queryWrapper.orderByDesc("id");

        // 执行分页查询
        Page<Log> logPage = logMapper.selectPage(page, queryWrapper);

        // 将 MyBatis-Plus 的 Page 转换为 PageInfo
        return logPage;
    }
    
    /**
     * 添加
     * @param log
     */
    public void add(Log log) {
        logMapper.insert(log);
    }
    
    /**
     * 修改
     * @param log
     */
    public void update(Log log) {
        logMapper.updateById(log);
    }
    
    /**
     * 根据id删除
     * @param id
     * @return
     */
    public void delete(Integer id) {
        logMapper.deleteById(id);
    }

}
